package com.weizhu.swufer.core.filter;

import cn.hutool.core.util.StrUtil;
import com.weizhu.swufer.constant.SwuferConstant;
import com.weizhu.swufer.core.annotation.SwuferFilter;
import com.weizhu.swufer.core.entity.InvokeParam;
import com.weizhu.swufer.core.rpc.RpcContext;

import java.util.UUID;

@SwuferFilter(type = {SwuferConstant.FILTER_TYPE_PROVIDER,SwuferConstant.FILTER_TYPE_CLIENT},order = 0)
public class GlobalTraceFilter implements Filter{
    @Override
    public Object doFilter(InvokeParam invokeParam, FilterChain filterChain) throws Exception {
        String traceId = (String)invokeParam.getAttachment(SwuferConstant.TRACE_ID);
        if (StrUtil.isBlank(traceId)){
            traceId = UUID.randomUUID().toString();
        }
        RpcContext.getContext().setParameter(SwuferConstant.TRACE_ID,traceId);
        return filterChain.doFilter(invokeParam);
    }
}
